package com.zan.config;

import com.zan.filter.JwtAuthenticationTokenFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * @Author Zan
 * @Date 2023/10/17 14:04
 * @Description : SpringSecurity的总配置文件
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) // 开启注解功能
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Resource
    private AccessDeniedHandler accessDeniedHandler;

    @Resource
    private AuthenticationEntryPoint authenticationEntryPoint;

    // 创建BCryptPasswordEncoder注入容器
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    // 认证都是在configure方法中
    @Override
    protected void configure(HttpSecurity http) throws Exception {
            // 都是通过HttpSecurity进行配置，每次都是返回一个HttpSecurity
            http
                // 关闭csrf
                .csrf().disable()
                // 不通过Session获取SecurityContext （STATELESS）从不会创建session，使用session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 请求认证的配置，下面都是放行登录接口的操作
                .authorizeRequests()
                // 对于登录接口，允许匿名访问，即未登录可以访问，已认证的不可以访问
                // anonymous(): 匿名访问：未登录可以访问，已登录不能访问
                // permitAll(): 无论是否登录，都可以访问
                .antMatchers("/user/login").anonymous()
                // 给/hello添加配置
//                .antMatchers("/hello").hasAuthority("system:dept:list")
                // 除上面外的所有请求全部都需要鉴权认证，即都需要登录
                .anyRequest().authenticated();

            // 将自定义的过滤器添加到过滤链中，并指定位置顺序
            http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

            // 配置异常处理器 - 自定义认证和授权的异常处理器
            http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
            http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);

            // 允许跨域
            http.cors();
    }
}
